package com.levy.contronller.javaWeb.ajaxCross;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 这里的配置为  接口服务器上的配置，这里仅作笔记使用  不能实际请求
 *
 * 跨域
 *
 * webXml配置
 *
 * <filter>
 * <filter-name>cors</filter-name>
 * <filter-class>com.gmcc.bi.interceptor.HeadersCORSFilter</filter-class>
 * </filter>
 * <filter-mapping>
 * <filter-name>cors</filter-name>
 * <url-pattern>/*</url-pattern>
 * </filter-mapping>
 *
 *
 */
public class HeadersCORSFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse servletResponse,
                         FilterChain chain) throws IOException, ServletException {
		 HttpServletResponse response = (HttpServletResponse) servletResponse;
         response.setHeader("Access-Control-Allow-Origin", "*");
         response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
         response.setHeader("Access-Control-Max-Age", "3600");
         response.setHeader("Access-Control-Allow-Headers",
				 "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With");
         response.setHeader("Access-Control-Allow-Credentials","true");
         chain.doFilter(request, servletResponse);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}
